import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/apartment/api.dart";
import "package:ct_pa_majordomo/pages/apartment/index.dart";
import "package:ct_pa_majordomo/pages/main.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:ct_pa_majordomo/widgets/wrap_item.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class AddBuilding extends StatefulWidget {
  final String buildingName;
  final List<Map> floors;
  final int groupId;
  const AddBuilding({super.key, required this.buildingName, required this.floors, required this.groupId});

  @override
  State<StatefulWidget> createState() => _AddBuildingState();

}

class _AddBuildingState extends State<AddBuilding> {

  bool _isExpanded = false;

  void _createBuilding(Map params) async {
    await apartmentApi.createBuilding(params);
    EasyLoading.showToast("添加成功");
    Stacker.pushAndRemoveUntil(const Main(index: 1,));
  }

  Widget _renderFloor(Map info) {
    return ExpansionTile(
      title: Text("${info["floor"]}层"),
      backgroundColor: Colors.white,
      collapsedBackgroundColor: Colors.white,
      iconColor: ThemeVars.colorTextTer,
      collapsedIconColor: ThemeVars.colorTextTer,
      collapsedShape: Border.all(color: ThemeVars.colorTextTer, width: 0),
      shape: Border.all(color: Colors.transparent, width: 0),
      children: [
        Container(
          width: MediaQuery.of(context).size.width,
          padding: const EdgeInsets.all(ThemeVars.paddingPage),
          decoration: const BoxDecoration(
              color: ThemeVars.colorBackground
          ),
          child: Wrap(
            spacing: 12,
            runSpacing: 12,
            children: List.generate(info["roomNameList"].length, (index) {
              return WrapItem(
                  span: 3,
                  child: Container(
                    padding: const EdgeInsets.symmetric(vertical: 6),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: const BorderRadius.all(Radius.circular(5)),
                        border: Border.all(color: ThemeVars.colorPrimary)
                    ),
                    child: Text(info["roomNameList"][index], style: const TextStyle(color: ThemeVars.colorPrimary),),
                  )
              );
            }),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "添加整栋",
      child: Stack(
        children: [
          ListView(
            padding: const EdgeInsets.only(bottom: 72),
            children: List.generate(widget.floors.length, (index) {
              return _renderFloor(widget.floors[index]);
            }).toList(),
          ),
          Positioned(
              bottom: 0,
              left: 0,
              width: MediaQuery.of(context).size.width,
              child: Container(
                padding: const EdgeInsets.all(ThemeVars.paddingPage),
                decoration: const BoxDecoration(
                  color: ThemeVars.colorBackground,
                ),
                child: Button(
                    type: ButtonType.primary,
                    onPressed: () {
                      Map params = {
                        "buildingName": widget.buildingName,
                        "groupId": widget.groupId,
                        "floors": widget.floors
                      };
                      _createBuilding(params);
                    },
                    child: const Text("确定")
                ),
              )
          ),
        ],
      ),
    );
  }

}